/**
 * Copyright (c) 2008 michiyasu wada
 * http://www.seyself.com/
 * 
 * Distributed under The MIT License.
 * [http://www.opensource.org/licenses/mit-license.php]
 *
 * @author michiyasu wada / michi@seyself.com
 */


dynamic class flash.utils.Proxy extends Object
{
    
    public function Proxy()
    {
        __init();
        _addProperties( arguments );
    }
    
    public function callProperty( name, args )
    {
        return null;
    }
    
    public function deleteProperty( name ):Boolean
    {
        return false;
    }
    
    public function getProperty( name )
    {
        return null;
    }
    
    public function hasProperty( name ):Boolean
    {
        for (var val in this)
            if (val == name) return true;
        return false;
    }
    
    public function setProperty( name , value ):Void
    {
        
    }
    
    public function nextName( index:Number ):String
    {
        var i:Number = 0;
        for (var val in this) {
            if (i == index) return val;
            i++;
        }
        return null;
    }
    
    public function nextNameIndex( index:Number ):Number
    {
        var i:Number = 0;
        for (var val in this) {
            if (i == index) return (i+1);
            i++;
        }
        return 0;
    }
    
    public function nextValue( index:Number ):Object
    {
        var i:Number = 0;
        for (var val in this) {
            if (i == index) return this[val];
            i++;
        }
        return null;
    }
    
    public function toString():String
    {
        return "[object Proxy]";
    }
    
    public function valueOf():Object
    {
        return this;
    }
    
    //                                                    s t a t i c
    //***************************************************************
    
    public static function forIn( target:Proxy ):String
    {
        target.__loopIndex = target.nextNameIndex( target.__loopIndex );
        if ( target.__loopIndex > 0) {
            return target.nextName( target.__loopIndex );
        }
        return null;
    }
    
    public static function forEach( target:Proxy ):Object
    {
        target.__loopIndex = target.nextNameIndex( target.__loopIndex );
        if ( target.__loopIndex > 0) {
            return { data : target.nextValue(target.__loopIndex) };
        }
        return null;
    }
    
    
    
    //                                                  p r i v a t e 
    //***************************************************************
    
    private var __loopIndex:Number = 0;
    
    private function __init():Void
    {
        var loc = this;
        this.__resolve = function( name )
        {
            var f = function()
            {
                callProperty( name, arguments );
            }
            return f;
        }
        _global.ASSetPropFlags( this, "__resolve", 1 );
    }
    
    private function _addProperties( args ):Void
    {
        var len = args.length;
        for (var i = 0; i < len;i++ ) {
            _addProperty( this, args[i] );
        }
    }
    
    private function _addProperty( target, name:String ):Void
    {
        var loc = target;
        var __setter__ = function( value )
        {
            loc.setProperty( name , value );
        }
        var __getter__ = function()
        {
            return loc.getProperty( name );
        }
        target.addProperty( name, __getter__, __setter__ );
    }
    
    
    
    
    
}
